const { getDayString } = require('../util')
Parse.Cloud.define('updateInstallCount', async (req, res) => {
  try {
    const day = getDayString()
    const findUseTotalCount = await new Parse.Query('QlUseTotalCount').first()
    const findUseCount = await new Parse.Query('QlUseCount').equalTo('day', day).first()
    const useTotalCount = await findUseTotalCount || new Parse.Object('QlUseTotalCount')
    const useCount = await findUseCount || new Parse.Object('QlUseCount')

    const totalInstall = useTotalCount.get('install') || 0
    const install = useCount.get('install') || 0

    useTotalCount.set('install', totalInstall + 1)
    useCount.set('day', day).set('install', install + 1)

    await Parse.Object.saveAll([useCount, useTotalCount])
    res.success('ok')
  } catch (err) {
    console.log(err)
    res.error(err.message)
  }
})

